<?php

namespace app\api\controller;

use app\api\model\Registered;
use app\common\controller\Api;
use think\Validate;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['yueList','registeredSubmit'];


    /**
     * 在线挂号提交
     *
     * @ApiTitle    (在线挂号提交)
     * @ApiSummary  (在线挂号提交)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=name, type=string, required=true, description="姓名")
     * @ApiHeaders  (name=mobile, type=string, required=true, description="手机号")
     * @ApiHeaders  (name=yuyue, type=string, required=true, description="来院时间")
     * @ApiHeaders  (name=doctor, type=string, required=true, description="预约医生名称")
     * @ApiHeaders  (name=project, type=string, required=true, description="预约项目名称")
     * @ApiHeaders  (name=desc, type=string, required=true, description="需求简述")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function registeredSubmit()
    {
        $uid=$this->auth->id;
        $name=$this->request->request('name','');
        $mobile=$this->request->request('mobile','');
        $yuyue=$this->request->request('yuyue','');
        $doctor=$this->request->request('doctor','');
        $project=$this->request->request('project','');
        $desc=$this->request->request('desc','');
        if (empty($name)) $this->error('请输入预约姓名');
        if (empty($mobile)) $this->error('请输入预约电话');
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式不正确');
        }
        if (empty($yuyue)||$yuyue=='请选择来院时间') $this->error('请选择来院时间');
        if (empty($doctor)||$doctor=='请选择预约科室') $this->error('请选择预约科室');
        if (empty($project)||$project=='请选择项目') $this->error('请选择预约项目');
//        if (empty($money)) $this->error('请输入挂号费');
        $data['name']=$name;
        $data['mobile']=$mobile;
        $data['yuyue']=$yuyue;
        $data['doctor']=$doctor;
        $data['project']=$project;
        $data['desc']=$desc;
        $data['user_id']=$uid;
        $data['createtime']=time();
        $res=Registered::create($data);
        if ($res) $this->success('挂号成功');
        else $this->error('挂号失败');
    }

    /**
     * 在线预约列表
     *
     * @ApiTitle    (在线预约列表)
     * @ApiSummary  (在线预约列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="用户token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function yueList()
    {
        $uid=$this->auth->id;
        $res=Registered::where('user_id',$uid)->order('id desc')->select();
        $this->success('请求成功',$res);
    }
}
